package com.example.xunua_mymusic.fragment;


import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xunua_mymusic.MainActivity;
import com.example.xunua_mymusic.R;
import com.example.xunua_mymusic.adapter.MyAdapterabc;
import com.example.xunua_mymusic.entitys.Music;
import com.example.xunua_mymusic.utils.GetMusicList;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import static android.content.ContentValues.TAG;

/**
 * A simple {@link Fragment} subclass.
 */
public class local_music_Fragment_first extends Fragment {
    ListView lv01,lv02;
    ArrayList<Music> musicList;
    MyAdapter myAdapter;
    ArrayList<Music> musics;
    MainActivity mainActivity;
    int index;

    public local_music_Fragment_first() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view=inflater.inflate(R.layout.fragment_local_music__fragment_first, container, false);
        lv01=view.findViewById(R.id.fragment_local_music_fragment_first_lv01);
        lv02=view.findViewById(R.id.fragment_local_music_fragment_first_lv02);
        lv02.setAdapter(new MyAdapterabc(getActivity()));

        mainActivity= (MainActivity) getActivity();
        musics = mainActivity.getMusic();
        musicList=new ArrayList<>();
        Music music=new Music();
        music.setTitle("随机播放全部");
        music.setArtist("共"+musics.size()+"首");
        musicList.add(music);
        musicList.addAll(musics);
        myAdapter = new MyAdapter();
        lv01.setAdapter(myAdapter);
        lv01.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position > 0) {
                    mainActivity.setIndex(position-1);
                    mainActivity.pause();
                    mainActivity.isPlayMusic();
                }
            }
        });

        return view;
    }
    private class  MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return musicList.size();
        }

        @Override
        public Object getItem(int position) {
            return musicList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder=null;
            if (convertView == null) {
                holder=new ViewHolder();
                convertView=getLayoutInflater().inflate(R.layout.local_music_first_lv_item,null);
                holder.tv_img=convertView.findViewById(R.id.local_music_first_lv_item_tv_img);
                holder.tv_ari=convertView.findViewById(R.id.local_music_first_lv_item_tv_ari);
                holder.tv_right=convertView.findViewById(R.id.local_music_first_lv_item_tv_right);
                holder.tv_title=convertView.findViewById(R.id.local_music_first_lv_item_tv_title);
                convertView.setTag(holder);
            }else {
                holder= (ViewHolder) convertView.getTag();
            }
            if (position == 0) {
                holder.tv_ari.setText(musicList.get(position).getArtist());
            }else {
                holder.tv_img.setText(position+"");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    holder.tv_img.setBackground(null);
                }else {
                    holder.tv_img.setBackgroundResource(0);
                }
//                Drawable drawabletop = getResources().getDrawable(R.mipmap.btn_channel_play_default);
//                holder.tv_img.setCompoundDrawables(null, null,null, null);
                Drawable drawablebottom = getResources().getDrawable(R.mipmap.img_album_right_arrow);
                holder.tv_right.setText("");
                holder.tv_right.setCompoundDrawablesWithIntrinsicBounds(null,null,drawablebottom,null);
                holder.tv_title.setText(musicList.get(position).getTitle());
                holder.tv_ari.setText(musicList.get(position).getArtist());
            }
            return convertView;
        }
        private class ViewHolder{
            TextView tv_img,tv_title,tv_ari,tv_right;
        }
    }
}


/**
 *              Drawable drawableLeft = getResources().getDrawable(R.mipmap.gengduo);
 *              viewHolder.tv_right.setCompoundDrawablesWithIntrinsicBounds(null,null,drawableLeft,null);
 */
